package com.estar.assistsurvy.activity;

import java.io.File;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.estar.assistsurvy.R;
import com.estar.assistsurvy.constant.Constants;
import com.estar.assistsurvy.utils.GlideCircleTransform;
import com.estar.assistsurvy.utils.GlideUtiles;
import com.estar.assistsurvy.widget.SelectPicPopupWindow;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class EWorkCardActivity extends BaseActivity implements OnClickListener{

	private ImageView headImg;
	private TextView realName,phoneNumber;
	private LinearLayout returnBack;
	private SharedPreferences ueserPrefer,urlPrefer;
	private String name,phone,dataSources,headUrl,picPath;
	private SelectPicPopupWindow ppw;
	private Intent intent;
	private RequestManager glideRequest;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_eworkcard);
		init();
	}
	private void init(){
		headImg=find(R.id.headImg);
		headImg.setOnClickListener(this);
		returnBack=find(R.id.returnBack);
		returnBack.setOnClickListener(this);
		realName=find(R.id.realName);
		phoneNumber=find(R.id.phoneNumber);
		urlPrefer=getSharedPreferences("headURL", MODE_PRIVATE);
		ueserPrefer=this.getSharedPreferences("user", Context.MODE_PRIVATE);
		dataSources=ueserPrefer.getString("dataSources", "");
		phone=ueserPrefer.getString("phoneNumber", "");
		phoneNumber.setText(phone);
		name=ueserPrefer.getString("realName", "");
		realName.setText(name);
		headUrl=ueserPrefer.getString("headUrl", "");
		glideRequest=Glide.with(this);
		if(dataSources.equals("0")){
			glideRequest.load(Constants.PIC_PATH+headUrl)
			.placeholder(R.drawable.icon_heder)
			.diskCacheStrategy(DiskCacheStrategy.ALL)
			.transform(new GlideCircleTransform(this)).into(headImg);
//			GlideUtiles.loadIntoUseFitWidth(this, Constants.PIC_PATH+headUrl, headImg,R.drawable.icon_heder);
		}else if(dataSources.equals("1")){
			picPath=urlPrefer.getString(phone, "");
			glideRequest.load(picPath).placeholder(R.drawable.icon_heder)
			.transform(new GlideCircleTransform(this)).into(headImg);
//			GlideUtiles.loadIntoUseFitWidth(this, picPath, headImg,R.drawable.icon_heder);
		}
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.headImg:
			if(dataSources.equals("1")){
				setPopwindow();
			}
			break;
			
		case R.id.returnBack:
			finish();
			break;

		default:
			break;
		}
	}
	
	private void setPopwindow(){
		ppw=new SelectPicPopupWindow(this, new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		}, new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//调用系统相机获取照片
				intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				long currentTime = System.currentTimeMillis();
				picPath=Environment
						.getExternalStorageDirectory()
						+ "/DCIM/"+"ESTAR"+currentTime + ".jpg";
				// 加载路径
				Uri uri = Uri.fromFile(new File(picPath));
				// 指定存储路径，这样就可以保存原图了
				intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
				// 拍照返回图片
				startActivityForResult(intent, 0);
				ppw.dismiss();
			}
		});
		// 设置ppw显示的位置
		ppw.showAtLocation(this
				.findViewById(R.id.headImg), Gravity.CENTER
				| Gravity.CENTER_HORIZONTAL, 0, 0);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode==RESULT_OK){
			if(requestCode==0){
				GlideUtiles.loadIntoUseFitWidth(this, picPath, headImg);
				Editor editor=urlPrefer.edit();
				editor.putString(phone, picPath);
				editor.commit();
			}
		}
	}
}
